require_dependency "search"
class Video < ActiveRecord::Base  
  belongs_to                  :photo
  has_many                    :comments
  has_many                    :partnerships
  has_many                    :items
  has_many                    :galleries,       :through => :partnerships
  has_many                    :orders,          :through => :items
  has_many                    :approvals
  has_attachment              :storage =>       :file_system,
                              :processor=>      :rmagick,
                              :content_type =>  ['video/quicktime', 'video/x-m4v', 'video/mp4', 'video/x-mpeg', 'video/mpeg'],
                              :max_size =>      30.megabytes
  validates_as_attachment
  validates_presence_of       :title
  searches_on                 :title,
                              :caption,
                              :description
  
  has_friendly_id             :title,            :use_slug => true
  
  before_save                 :make_purchasable_if_part_of_purchase_gallery
  after_save                  :create_partnerships
  after_destroy               :destroy_dependents
  
  def is_purchasable?
    !self.non_purchasable?
  end
  
  def in_more_than_one_gallery?
    self.galleries.length > 1
  end
  
  def sanitized_caption
    self.caption ? (self.caption.gsub(/\\|'/) { |c| "\\#{c}" }.gsub("\r","").gsub("\n","")) : ""
  end
  
  def is_invisible?
    !self.galleries.detect{|gallery| gallery.invisible?}.nil?
  end
  
  def thumbnail
    self.photo.nil? ? '/images/assets/default_thumb.jpg' : self.photo.public_filename(:small)
  end
  
  def approval_for(gallery, current_user)
    Approval.find(:all, :conditions => ["video_id = #{self.id} AND gallery_id = #{gallery.id} AND user_id = #{current_user.id} AND approved = ?", true]).first
  end
  
  def approval_in(gallery)
    Approval.find(:all, :conditions => ["video_id = #{self.id} AND gallery_id = #{gallery.id}"]).first
  end
  
  def approved_in?(gallery)
    Approval.find(:all, :conditions => ["video_id = #{self.id} AND gallery_id = #{gallery.id} AND approved = ?", true]).first ? true : false
  end
  
  def declined_in?(gallery)
    Approval.find(:all, :conditions => ["video_id = #{self.id} AND gallery_id = #{gallery.id} AND declined = ?", true]).first ? true : false
  end
  
  def needs_review_in?(gallery)
    return true if self.approvals.empty?
    self.approvals.each {|approval| !gallery.approvals.include?(approval) ? true : false}
  end
  
  def users
    self.galleries.empty? ? [] : ((users = self.galleries.collect {|gallery| gallery.users unless gallery.users.empty?}.reject {|item| item.nil?}.flatten) ? users : [])
  end

  def gallery_ids=(gallery_ids = [])
    @gallery_ids = gallery_ids
  end

protected
  def create_partnerships
    self.partnerships.each {|partnership| partnership.destroy if partnership.gallery and @gallery_ids} if self.partnerships
    @gallery_ids.each {|gallery_id| Partnership.create!(:video_id => self.id, :gallery_id => gallery_id)} unless @gallery_ids.nil?
  end

  def destroy_dependents
    self.comments.each {|comment| comment.destroy} unless self.comments.empty?
    self.partnerships.each {|partnership| partnership.destroy} unless self.partnerships.empty?
    self.approvals.each {|approval| approval.destroy} unless self.approvals.empty?
    self.items.each {|item| item.destroy} unless self.items.empty?
  end
  
  def make_purchasable_if_part_of_purchase_gallery
    @gallery_ids.each {|gallery_id| self.is_purchasable = true if Gallery.find(gallery_id).purchasable?} unless @gallery_ids.nil?
  end
end
